package com.zhaosc.elasticsearch;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import com.google.common.collect.Maps;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-index.html
 * @author zhaosc
 *
 */
public class IndexTest extends BaseTest{

	private static final Logger logger = Logger.getLogger(IndexTest.class);


	/**
	 * 第一种方式map
	 */
	@Test
	public void testIndex01() {
		Map<String, String> source = Maps.newHashMap();
		source.put("gender", "man");
		source.put("age", "28");
		source.put("xingming", "zhaosd2");
		IndexRequest indexRequest = new IndexRequest(index);
		
		indexRequest.source(source);
		RequestOptions options = RequestOptions.DEFAULT;

		try {
			IndexResponse indexResponse = client.index(indexRequest, options);
			Result result = indexResponse.getResult();
			if(Result.CREATED==result) {
				logger.info("****success****");
			}else {
				logger.info("****fail****");
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 第二种以json字符串形式
	 */
	@Test
	public void testIndex02() {
		IndexRequest indexRequest = new IndexRequest(index);
		indexRequest.id("1");
		String jsonString = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
				+ "\"message\":\"trying out Elasticsearch\"" + "}";
		indexRequest.source(jsonString, XContentType.JSON);
		RequestOptions options = RequestOptions.DEFAULT;

		try {
			IndexResponse indexResponse = client.index(indexRequest, options);
			Result result = indexResponse.getResult();
			if(Result.CREATED==result) {
				logger.info("****success****");
			}else {
				logger.info("****fail****");
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 第三种XContentBuilder形式
	 */
	@Test
	public void testIndex03() {
		IndexRequest indexRequest = new IndexRequest(index);
		try {
			XContentBuilder builder = XContentFactory.jsonBuilder();
			builder.startObject();
			{
				builder.field("user", "kimchy");
				builder.timeField("postDate", new Date());
				builder.field("message", "haha");
			}
			builder.endObject();
			indexRequest.id("1").source(builder);
			RequestOptions options = RequestOptions.DEFAULT;

			IndexResponse indexResponse = client.index(indexRequest, options);
			Result result = indexResponse.getResult();
			if(Result.CREATED==result) {
				logger.info("****success****");
			}else {
				logger.info("****fail****");
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 第四种以key-value形式赋值
	 */
	@Test
	public void testIndex04() {
		IndexRequest indexRequest = new IndexRequest(index);

		indexRequest.id("1").source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch");
		try {
			RequestOptions options = RequestOptions.DEFAULT;
			IndexResponse indexResponse = client.index(indexRequest, options);
			Result result = indexResponse.getResult();
			if(Result.CREATED==result) {
				logger.info("****success****");
			}else {
				logger.info("****fail****");
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}


}
